using Echo.Context.UltiProNet.Contexts;
using Echo.Context.UltiProNet.Macros.Database.PayrollHelpers;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.TestSupport.Service;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by: Dionny Santiago
////Date Created: October 23, 2012
////Modified by: Ruben A. Martinez
////Date modified: January 14. 2014
////Project: ASP.NET Payroll Site Admin Smoke Test
////Reason: ULTI-120477 Using new pay groups in SB340 in order to avoid data corruption in build (Pay Group: PSTBI & PSTSM)
////*************************************************************************************************************************************

namespace Echo.Automation.Payroll
{
    [TestFixture]
    [Header("Config")]
    [Config(".NET")]
    [Author("Dionny Santiago", "dionny_santiago@ultimatesoftware.com")]
    [Category("BADSmokeTest")]
    [Category("Payroll Processing")]
    [Timeout(2400)]
    public class PayrollSiteAdmin : AutomatedTest
    {
        const string paygroup = "PSTBI";
        const string paygroup2 = "PSTSM";
        const string loginUserName = "susann";
        const string clientID = "LASB";
        const int payrollModelID = 500004;
        const string nextperiodcontrol = "201403141";
        const string nextperiodcontrol2 = "201403141";
        const string templateCode = "9XH9JN0000K0";

        UltiPro UltiPro;
        UltiProDatabaseContext Database;
        PayrollTearDown PSTBI;
        PayrollTearDown PSTSM;

        [FixtureSetUp]
        public void SetUp()
        {
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_SB340");
            Database.Site = ConfigDatabase("HRMS_SITE", Environment.UltiPro.SiteServer);

            // Add rollbacks.
            Database.Company.AddRollBack("paygroup", "pgrpaygroup in ('PSTBI', 'PSTSM')");

            // Updates no longer needed due to fixes made in the code and changes made to the setup server
            // Database.Company.Execute("UPDATE pyGrComp set PgcDateMonthReset = '2014-01-31 00:00:00.000' where  PgcPayGroup = 'PSTBI'");
            // Database.Company.Execute("UPDATE pyGrComp set PgcDateMonthReset = '2014-01-31 00:00:00.000' where  PgcPayGroup = 'PSTSM'");

            // Inserts the paygroup model data if it is not already present.
            Database.Company.Execute("delete from paygroupmodel where pgmpaygroup = 'PSTBI' AND pgmPayrollType = 'R'");
            Database.Company.Execute("delete from paygroupmodel where pgmpaygroup = 'PSTSM' AND pgmPayrollType = 'R'");
            Database.Company.Execute("insert paygroupmodel (pgmPayGroup, pgmPayrollModelID, pgmPayrollType, pgmTemplateCode) VALUES ('PSTBI', '500004', 'R', '9XH9JN0000K0')");
            Database.Company.Execute("insert paygroupmodel (pgmPayGroup, pgmPayrollModelID, pgmPayrollType, pgmTemplateCode) VALUES ('PSTSM ', '500004', 'R', '9XH9JN0000K0')");

            PSTBI = Database.Macros.PayrollProcessing.PayrollSetup(paygroup, payrollModelID, templateCode, "R", clientID, loginUserName, this);
            PSTSM = Database.Macros.PayrollProcessing.PayrollSetup(paygroup2, payrollModelID, templateCode, "R", clientID, loginUserName, this);

            // Ensure the big three services have been stopped/started
            //ServiceHelper.Stop(Environment.UltiPro.UesServer.ToString(), "UESService");
            //ServiceHelper.Start(Environment.UltiPro.UesServer.ToString(), "UESService");
            //ServiceHelper.Stop(Environment.UltiPro.UesServer.ToString(), "DistributedProcessManager");
            //ServiceHelper.Start(Environment.UltiPro.UesServer.ToString(), "DistributedProcessManager");
            //ServiceHelper.Stop(Environment.UltiPro.UesServer.ToString(), "UltimateSoftware.FileManagement.WindowsService");
            //ServiceHelper.Start(Environment.UltiPro.UesServer.ToString(), "UltimateSoftware.FileManagement.WindowsService");

            // Launch the browser
            UltiPro = Launch<UltiPro>();

            UltiPro.Pages.Login.LoginAs(loginUserName, "password", Config.LanguageCode);
            UltiPro.Pages.CompanySelector.ChooseCompanies("LASB", "PSTCO");

            UltiPro.Menu.PayrollProcessing.Click(link => link.PayrollGateway);

            // Verify page header
            UltiPro.Pages.PayrollGateway.Is(control => control.PageHeaderLabel, "innerHtml", "Payroll Gateway");

            // Expand to view the full page
            UltiPro.Pages.PayrollGateway.Click(page => page.FullPageView);

            // Verify page header
            UltiPro.Pages.ProcessPay.Is(control => control.PageHeaderLabel, "innerHtml", "Process Pay");

            // View by Status
            UltiPro.Pages.ProcessPay.Click(page => page.ViewByStatusButton);

            // Expand the Start payroll section
            UltiPro.Pages.ProcessPay.Click(page => page.StartPayrollCollapsibleSection);

            // Verify the Pay Group Process Alerts section exists
            UltiPro.Pages.ProcessPay.Exists(page => page.PayGroupProcessingAlertsCollapsibleSection);
        }

        [FixtureTearDown]
        public void TearDown()
        {
            UltiPro.Browser.Quit();

            PSTBI.TearDownAllDataWithRollback();
            PSTSM.TearDownAllDataWithRollback();
            Database.Company.RollBackDatabase();
            // Updates no longer needed due to fixes made in the code and changes made to the setup server
            // Database.Company.Execute("UPDATE pyGrComp set PgcDateMonthReset = '2014-01-31 00:00:00.000' where  PgcPayGroup = 'PSTBI'");
            // Database.Company.Execute("UPDATE pyGrComp set PgcDateMonthReset = '2014-01-31 00:00:00.000' where  PgcPayGroup = 'PSTSM'");
        }

        [Test(Order = 1)]
        public void Verify_the_user_can_see_all_paygroups()
        {
            ////*************************************************************************************************************
            ////      Test Case 1: Verify the user can see all paygroups
            ////      Test Data: susann/password , Pay Groups all in StartPayrollGrid: Payroll Smoke Tests Biwk, Payroll Smoke Tests Semi, WorkPlace Biweekly
            ////      Anticipated Results: The user can see all paygroups
            ////*************************************************************************************************************

            // Verify Paygroup Payroll Smoke Tests Biwk exists.
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").RowExists();

            // Verify Paygroup Payroll Smoke Tests Semi exists.
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").RowExists();

            // Verify Tips WorkPlace Biweekly exists.
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "WorkPlace Biweekly").RowExists();
        }

        [Test(Order = 2)]
        [Timeout(1200)]
        public void Verify_the_user_can_open_a_payroll()
        {
            ////*************************************************************************************************************
            ////      Test Case 2: Verify the user can open a payroll
            ////      Test Data: susann/password , Pay Groups: Payroll Smoke Tests Biwk, Payroll Smoke Tests Semi
            ////      Anticipated Results: The user can open a payroll
            ////*************************************************************************************************************

            // Select Paygroup AUTO PTO Weekly.
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").Click(row => row.CheckBox);

            // Select Paygroup AUTO PTO2 Bi-weekly Monthly.
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").Click(row => row.CheckBox);

            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").Selected(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.StartPayrollGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").Selected(row => row.CheckBox);

            // Click the Run button.
            UltiPro.Pages.ProcessPay.ButtonBar.Click(bar => bar.Save);

            // Verify the processes have completed.
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup2, PSTSM.PerControl, "ECF60693-40CC-4216-ADB4-118989A2DAC6");
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup, PSTBI.PerControl, "ECF60693-40CC-4216-ADB4-118989A2DAC6");
            Database.Company.VerifyValue("select pgrispayrollopen from paygroup where pgrpaygroup = 'PSTBI'", "Y");
            Database.Company.VerifyValue("select pgrispayrollopen from paygroup where pgrpaygroup = 'PSTSM'", "Y");

            // Collapse the Start payroll collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.StartPayrollCollapsibleSection);
        }

        [Test(Order = 3)]
        public void Verify_a_site_admin_can_bring_in_time()
        {
            ////*************************************************************************************************************
            ////      Test Case 3: Verify a site admin can bring in time
            ////      Test Data: susann/password , Pay Groups: Payroll Smoke Tests Biwk, Payroll Smoke Tests Semi
            ////      Anticipated Results: A site admin can bring in time.
            ////*************************************************************************************************************

            // Hit the refresh button.
            UltiPro.Pages.ProcessPay.ButtonBar.ClickAndWaitForPostBack(bar => bar.Reset);

            // Expand the Bring in time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.BringInTimeCollapsibleSection);

            // Bring in time for the payroll.
            UltiPro.Pages.ProcessPay.BringInTimeGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.BringInTimeGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.BringInTimeGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").Selected(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.BringInTimeGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").Selected(row => row.CheckBox);

            // Click the Run button.
            UltiPro.Pages.ProcessPay.ButtonBar.Click(bar => bar.Save);

            // Verify the processes have completed.
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup2, PSTSM.PerControl, "F8E6427E-4DAC-4C39-BF1D-B02E90F191F7");
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup, PSTBI.PerControl, "F8E6427E-4DAC-4C39-BF1D-B02E90F191F7");
            Database.Company.VerifyRecordCount("select top 1 * From tcwkern where tcepaygroup = 'PSTBI'", 1, 120);
            Database.Company.VerifyRecordCount("select top 1 * From tcwkern where tcepaygroup = 'PSTSM'", 1, 120);

            // Collapse the Bring in time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.BringInTimeCollapsibleSection);
        }

        [Test(Order = 4)]
        [Timeout(1200)]
        public void Verify_a_user_can_perform_verify_time()
        {
            ////*************************************************************************************************************
            ////      Test Case 4: Verify a user can perform verify time
            ////      Test Data: susann/password , Pay Groups: Payroll Smoke Tests Biwk, Payroll Smoke Tests Semi
            ////      Anticipated Results: A user can perform verify time
            ////*************************************************************************************************************

            // Hit the refresh button.
            UltiPro.Pages.ProcessPay.ButtonBar.ClickAndWaitForPostBack(bar => bar.Reset);

            // Expand the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.VerifyTimeCollapsibleSection);

            // Verify time for the payroll.
            UltiPro.Pages.ProcessPay.VerifyTimeGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.VerifyTimeGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.VerifyTimeGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").Selected(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.VerifyTimeGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").Selected(row => row.CheckBox);

            // Click the Run button.
            UltiPro.Pages.ProcessPay.ButtonBar.Click(bar => bar.Save);

            // Verify the processes have completed.
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup2, PSTSM.PerControl, "8ED09192-6AE0-41DB-83C9-1118561BF56A"); 
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup, PSTBI.PerControl, "8ED09192-6AE0-41DB-83C9-1118561BF56A");
            
            // Collapse the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.VerifyTimeCollapsibleSection);
        }

        [Test(Order = 5)]
        [Timeout(1200)]
        public void Verify_a_site_admin_can_select_Submit_Time_and_Calculate()
        {
            ////*************************************************************************************************************
            ////      Test Case 5: Verify a site admin can select Submit Time and Calculate
            ////      Test Data: susann/password , Pay Groups: Payroll Smoke Tests Biwk, Payroll Smoke Tests Semi
            ////      Anticipated Results: A user can perform submit time and calculate
            ////*************************************************************************************************************

            // Hit the refresh button.
            UltiPro.Pages.ProcessPay.ButtonBar.ClickAndWaitForPostBack(bar => bar.Reset);

            // Expand the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.SubmitTimeAndCalculateCollapsibleSection);

            // Submit time and calculate for the payroll.
            UltiPro.Pages.ProcessPay.SubmitTimeAndCalculateGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.SubmitTimeAndCalculateGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.SubmitTimeAndCalculateGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").Selected(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.SubmitTimeAndCalculateGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").Selected(row => row.CheckBox);

            // Click the Run button.
            UltiPro.Pages.ProcessPay.ButtonBar.Click(bar => bar.Save);

            // Verify the processes have completed.
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup2, PSTSM.PerControl, "F99694A2-4D7B-4474-BD3C-517CF815F208"); 
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup, PSTBI.PerControl, "F99694A2-4D7B-4474-BD3C-517CF815F208");
            Database.Company.VerifyRecordCount("select top 1 * from m_batch where mbtPayGroup = 'PSTBI'", 1, 120);
            Database.Company.VerifyRecordCount("select top 1 * from m_batch where mbtPayGroup = 'PSTSM'", 1, 120);

            // Collapse the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.SubmitTimeAndCalculateCollapsibleSection);
        }

        [Test(Order = 6)]
        [Timeout(1200)]
        public void Verify_a_site_admin_can_Print_and_Finish_a_payroll()
        {
            ////*************************************************************************************************************
            ////      Test Case 6: Verify a site admin can Print and Finish a payroll
            ////      Test Data: susann/password , Pay Groups: Payroll Smoke Tests Biwk, Payroll Smoke Tests Semi
            ////      Anticipated Results: A user can Print and Finish a payroll
            ////*************************************************************************************************************

            // Hit the refresh button.
            UltiPro.Pages.ProcessPay.ButtonBar.ClickAndWaitForPostBack(bar => bar.Reset);

            // Expand the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.PrintPayrollCollapsibleSection);

            // Print the payroll.
            UltiPro.Pages.ProcessPay.PrintPayrollGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.PrintPayrollGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").Click(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.PrintPayrollGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Biwk").Selected(row => row.CheckBox);
            UltiPro.Pages.ProcessPay.PrintPayrollGrid.Row(row => row.PayrGroupLink, "Payroll Smoke Tests Semi").Selected(row => row.CheckBox);

            // Click the Run button.
            UltiPro.Pages.ProcessPay.ButtonBar.Click(bar => bar.Save);

            // Verify the processes have completed.
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup2, PSTSM.PerControl, "B14D0AB0-A51C-433C-9278-C53A44EB1C24"); 
            Database.Macros.PayrollProcessing.CheckDispatchForCompletedPayrollJob(paygroup, PSTBI.PerControl, "B14D0AB0-A51C-433C-9278-C53A44EB1C24");
            
            // Verify database records for paygroup PSTBI
            Database.Company.VerifyRecordCount("select * from m_batch where MbtPayGroup = 'PSTBI'", 0);
            Database.Company.VerifyValue("select PgrIsPayRollOpen from paygroup where pgrpaygroup = 'PSTBI'", "N");
            Database.Company.VerifyValue("select pgrperiodcontrol from paygroup where pgrpaygroup = 'PSTBI'", nextperiodcontrol);

            // Verify database records for paygroup PSTSM
            Database.Company.VerifyRecordCount("select * from m_batch where MbtPayGroup = 'PSTSM'", 0);
            Database.Company.VerifyValue("select PgrIsPayRollOpen from paygroup where pgrpaygroup = 'PSTSM'", "N");
            Database.Company.VerifyValue("select pgrperiodcontrol from paygroup where pgrpaygroup = 'PSTSM'", nextperiodcontrol2);

            // Collapse the Verify time collapsable section.
            UltiPro.Pages.ProcessPay.Click(page => page.PrintPayrollCollapsibleSection);
        }
    }
}
